package cfdt.ldl.trade.service.lawyer;

import cfdt.common.enums.CommonCodeEnum;
import cfdt.common.exception.CodeException;
import cfdt.common.vo.ObjectBaseResponse;
import cfdt.ldl.db.biz.LawyerInfoBiz;
import cfdt.ldl.core.entity.LawyerInfo;
import cfdt.tools.util.GUID;
import cfdt.ldl.trade.api.request.AddLawyerTradeRequest;
import cfdt.ldl.trade.api.request.GetLawyerInfoListTradeRequest;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 黑名单检查-实现类
 *
 * <pre>
 * Modify Information:
 * Author       Date          Description
 * ============ ============= ============================
 * wangxiao      2021年8月16日下午2:21:57      创建实现类
 *
 * </pre>
 */
@Service
public class LawyerTradeService {

    @Autowired
    private LawyerInfoBiz lawyerInfoBiz;

    public ObjectBaseResponse getLawyerInfoList(GetLawyerInfoListTradeRequest request) {
        QueryWrapper<LawyerInfo> queryWrapper = new QueryWrapper();
        queryWrapper.like(StrUtil.isNotEmpty(request.getName()), "NAME", request.getName());
        List<LawyerInfo> list = lawyerInfoBiz.list(queryWrapper);

        // 响应
        ObjectBaseResponse baseResponse = new ObjectBaseResponse();
        baseResponse.setData(CollUtil.isEmpty(list) ? new ArrayList<>() : list);
        return baseResponse;
    }

    public ObjectBaseResponse addLawyer(AddLawyerTradeRequest request) throws Exception {
        ObjectBaseResponse baseResponse = new ObjectBaseResponse();

        // 业务逻辑校验
        // ...


        LawyerInfo lawyerInfo = new LawyerInfo();
        lawyerInfo.setSystemNo(GUID.getTxNo27());
        // 属性拷贝
        BeanUtils.copyProperties(request, lawyerInfo);
        // 插入
        boolean saved = lawyerInfoBiz.save(lawyerInfo);
        if (!saved) {
            throw new CodeException(CommonCodeEnum.CODE_2004);
        }
        // 响应
        return baseResponse;
    }

}
